package com.soft.mainsite.component.beetl.tag.cms;

import com.github.pagehelper.PageInfo;
import com.soft.mainsite.common.utils.StrUtil;
import com.soft.mainsite.module.web.cms.service.SiteService;
import com.soft.mainsite.module.web.cms.service.TopicService;
import com.soft.mainsite.common.exception.CmsException;
import com.soft.mainsite.common.utils.CmsUtil;
import com.soft.mainsite.module.web.cms.service.ContentService;
import com.soft.mainsite.mybatis.model.TCmsSite;
import com.soft.mainsite.mybatis.model.TCmsTopic;
import org.beetl.core.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Description:专题类容列表标签
 *
 * @author Jin
 * @create 2017-05-26
 **/
@Service
@Scope("prototype")
public class TopicContentTag extends GeneralVarTagBinding {

    @Autowired
    private ContentService contentService;

    @Autowired
    private SiteService siteService;

    @Autowired
    private TopicService topicService;

    @Value("${system.site.subfix}")
    private String siteSubfix;

    @Value("${system.site.prefix}")
    private String sitePrefix;

    @Override
    public void render() {
        Integer titleLen = Integer.parseInt((String) this.getAttributeValue("titleLen"));
        Integer siteId = (this.getAttributeValue("siteId") instanceof String) ? Integer.parseInt((String) this.getAttributeValue("siteId")) : (Integer) this.getAttributeValue("siteId");
        Integer isHot = Integer.parseInt((String) this.getAttributeValue("isHot"));
        Integer isPic = Integer.parseInt(CmsUtil.isNullOrEmpty(this.getAttributeValue("isPic")) ? "3" : (String) this.getAttributeValue("isPic"));
        Integer isRecommend = Integer.parseInt(CmsUtil.isNullOrEmpty(this.getAttributeValue("isRecommend")) ? "0" : (String) this.getAttributeValue("isRecommend"));
        Integer orderBy = Integer.parseInt((String) this.getAttributeValue("orderBy"));
        Integer size = Integer.parseInt((String) this.getAttributeValue("size"));
        Integer topicId = this.getAttributeValue("topicId") instanceof String ? Integer.parseInt((String) this.getAttributeValue("topicId")) : (Integer) this.getAttributeValue("topicId");
        TCmsTopic topic = topicService.findById(topicId);
        if (CmsUtil.isNullOrEmpty(topic)) {
            return;
        }
        /*将专题中的categoryIds 字符串转为Long数组*/
        String[] str1 = topic.getCategoryIds().split(",");
        Long[] str2 = new Long[str1.length];
        for (int i = 0; i < str1.length; i++) {
            if (!"".equals(str1[i])) {
                str2[i] = Long.valueOf(str1[i]);
            }
        }
        PageInfo<Map> pageInfo = contentService.findTopicContentListBySiteIdAndCategoryIds(siteId, str2, orderBy, size, isHot, isPic, isRecommend);
        if (CmsUtil.isNullOrEmpty(pageInfo.getList())) {
            return;
        }
        try {
            wrapRender(pageInfo.getList(), titleLen, siteId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CmsException("TopicContentTag wrapRender异常 == " + e.getMessage());

        }

    }

    private void wrapRender(List<Map> contentList, int titleLen, int siteId) {
        HttpServletRequest request = (HttpServletRequest) ctx.getGlobal("request");
        String staticHtmlPath = ctx.getGlobal("staticHtmlPath") == null ? "" : (String) ctx.getGlobal("staticHtmlPath");
        int i = 1;
        for (Map content : contentList) {
            String title = content.get("title").toString();
            int length = title.length();
            if (length > titleLen) {
                content.put("title", title.substring(0, titleLen) + "...");
            }
            if (StrUtil.isBlank(content.get("url").toString())) {
                TCmsSite site = siteService.findById(siteId);
                if (CmsUtil.isNullOrEmpty(site)) {
                    throw new CmsException("站点不存在[siteId:" + siteId + "]");
                }
                String url = request.getContextPath() + staticHtmlPath + "/" + site.getSiteId() + "/";
                url += content.get("category_id") + "/" + content.get("content_id");
                content.put("url", url + siteSubfix);
            }
            content.put("index", i);
            this.binds(content);
            this.doBodyRender();
            i++;
        }

    }


}
